package com.example.sanchuang_demo.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sanchuang_demo.domain.School;
import com.example.sanchuang_demo.mapper.SchoolMapper;
import com.example.sanchuang_demo.web.dto.RequestParam;
import com.example.sanchuang_demo.web.dto.ResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/sc_demo/school")
@CrossOrigin
@Slf4j
public class SchoolController {
    @Resource
    private SchoolMapper schoolMapper;
    @CrossOrigin
    @PostMapping("/list")
    public ResponseDto<?> list(@RequestBody RequestParam param){
        if(param.pageSize==null||param.pageNum==null){
            return new ResponseDto<>(false,"缺少pageSize或者pageNum字段",null);
        }
        Page<School> page=new Page<>(param.pageNum,param.pageSize);
        QueryWrapper<School> queryWrapper=new QueryWrapper<>();
        if(param.getPageNum()!=null)
            queryWrapper.like("school_name",param.keyword);
        return new ResponseDto<>(true,null, schoolMapper.selectPage(page,queryWrapper));

    }
    @PostMapping("add")
    public ResponseDto<?> add(@RequestBody School school){
        if(school.getSchoolName()==null){
            return new ResponseDto<>(false,"学校/公司不能为空",null);
        }
        schoolMapper.insert(school);
        return new ResponseDto<>(true,null,"添加成功！");

    }
    @GetMapping("/delete")
    public ResponseDto<?> delete(@org.springframework.web.bind.annotation.RequestParam("id") String id){
        if(id==null)
            return new ResponseDto<>(false,"删除失败！",null);
        schoolMapper.deleteById(id);
        return new ResponseDto<>(true,null,"删除成功！");
    }
    @PostMapping("/modify")
    public ResponseDto<?> modify(@RequestBody School school){
        if(school.getSchoolName()==null)
            return new ResponseDto<>(false,"学校/公司不能为空！",null);
        schoolMapper.updateById(school);
        return new ResponseDto<>(true,null,"修改成功！");
    }

}
